/**
 * 
 */
package com.sip.comadapter.executor;

import java.util.ArrayList;
import java.util.List;

import org.aspectj.org.eclipse.jdt.internal.compiler.ast.ForeachStatement;

import com.sip.comadapter.common.BaseExecutor;
import com.sip.comadapter.executor.common.Executor;
import com.sip.comserver.communication.CommunicationMessage;
import com.sip.dbadapter.dao.domain.ApiDefinition;
import com.sip.dbadapter.dao.domain.ConsumeTemplate;
import com.sip.dbadapter.dao.interfaces.ConsumeTemplateSelectDao;
import com.sip.dbadapter.dao.utility.DaoLoadFactory;
import com.sip.dbadapter.dao.interfaces.ApiDao;

import com.sip.dbadapter.dao.utility.DaoNameConstant;
/**
 * @author da40188
 *
 */
public class TemplateSelectionExecutor extends BaseExecutor implements Executor {

	public CommunicationMessage execute(CommunicationMessage message)  throws Exception{
		// TODO Auto-generated method stub
		
		System.out.println("<<<<<<<<<<<<<<< TemplateSelectExecutor >>>>>>>>>>>>>>>");
		ConsumeTemplate consumeTemplate=(ConsumeTemplate)message.getObject();
		
		ConsumeTemplateSelectDao consumeTemplateDao=loadConsumeTemplateDao();
		System.out.println(consumeTemplate.getTemplateId().length()+"<<<<<<<<<<<<<"+consumeTemplate.getScanTypeWs().getScanTypeId());
		List templateList=null;
		long apiId=0;
		ApiDao apiDaoObj=loadApiDefinitionDao();
		
		if(consumeTemplate.getScanTypeWs().getScanTypeId()>0)
			 apiId=consumeTemplate.getScanTypeWs().getScanTypeId();
		if(consumeTemplate.getScanTypeWs().getScanTypeId()>0)
			 apiId=consumeTemplate.getScanTypeWs().getScanTypeId();
		if(consumeTemplate.getScanTypeWs().getScanTypeId()>0)
			 apiId=consumeTemplate.getScanTypeWs().getScanTypeId();
		if((consumeTemplate.getTemplateId()!=null & consumeTemplate.getTemplateId().length()>0) & (consumeTemplate.getScanTypeWs().getScanTypeId()>0 || consumeTemplate.getScanTypeRest().getScanTypeId()>0 || consumeTemplate.getScanTypeRmi().getScanTypeId()>0))
	        templateList=(List)consumeTemplateDao.getConsumeTemplateData(consumeTemplate.getTemplateId(),apiId);
		else if(consumeTemplate.getTemplateId()!=null & (consumeTemplate.getScanTypeWs().getScanTypeId()<0 || consumeTemplate.getScanTypeRest().getScanTypeId()<0 || consumeTemplate.getScanTypeRmi().getScanTypeId()<0))
			templateList=(List)consumeTemplateDao.getConsumeTemplateData(consumeTemplate.getTemplateId());
		else if((consumeTemplate.getTemplateId()!=null || consumeTemplate.getTemplateId().length()==0) & (consumeTemplate.getScanTypeWs().getScanTypeId()>0 || consumeTemplate.getScanTypeRest().getScanTypeId()>0 || consumeTemplate.getScanTypeRmi().getScanTypeId()>0))
			templateList=(List)consumeTemplateDao.getConsumeTemplateData(apiId);
		ApiDefinition apiDef=null;
		if(apiId!=0)
		   apiDef= apiDaoObj.findById(apiId);
				 String test=null;
		 ConsumeTemplate consumeTemplateObj=null;
		 List<ConsumeTemplate> consumeListFormatted=new ArrayList<ConsumeTemplate>();
		 for(Object Obj:templateList){
			  consumeTemplateObj=(ConsumeTemplate)Obj;
			 consumeTemplateObj.setApiName(apiDef.getName());
			   consumeListFormatted.add(consumeTemplateObj);
		 }
		 		 message.setObject(consumeListFormatted);
		List<ConsumeTemplate> consumeList=(List<ConsumeTemplate> ) message.getObject();
		return message;
	}
	
	
	   private ConsumeTemplateSelectDao loadConsumeTemplateDao() {

		   ConsumeTemplateSelectDao consumeTemplateDao = null;
	        try {
	        	consumeTemplateDao = (ConsumeTemplateSelectDao) DaoLoadFactory.getDao("SELECT_TEMPLATE");
	        } catch (Exception e) {

	            e.printStackTrace();
	        }
	        return consumeTemplateDao;
	    }
	   
	   private ApiDao loadApiDefinitionDao() throws Exception {

			  ApiDao apiDef=(ApiDao)DaoLoadFactory.getDao(DaoNameConstant.API_DAO);
			  return apiDef;
			  
			  
		   
		  }

}
